{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b0f23fb1",
   "metadata": {},
   "source": [
    "------------\n",
    "* 2022/11/12/week 12\n",
    "* Python学习记录\n",
    "* 记录人：wu-huichun\n",
    "-----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a8183cb",
   "metadata": {},
   "source": [
    "# 利用函数重用代码"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39d53695",
   "metadata": {},
   "source": [
    "### 一.用“def”命名代码块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "af637624",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:wu\n",
      "u\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "898c924e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:ling\n",
      "i\n"
     ]
    }
   ],
   "source": [
    "\"\"\"使用def关键字指示函数开始\"\"\"\n",
    "def search4vowels():\n",
    "    \"\"\"Display any vowels found in an asked-for word.\"\"\"  # 为函数代码增加一个docstring，简要描述函数的用途\n",
    "    vowels = set('aeiou')\n",
    "    word = input(\"Provide a word to search for vowels:\")\n",
    "    found = vowels.intersection(set(word))\n",
    "    for vowel in found:\n",
    "        print(vowel)\n",
    "search4vowels()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f9f3234",
   "metadata": {},
   "source": [
    "### 二、函数接收参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9c7059b8",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "search4vowels() missing 1 required positional argument: 'word'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [4]\u001b[0m, in \u001b[0;36m<cell line: 7>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m vowel \u001b[38;5;129;01min\u001b[39;00m found:\n\u001b[0;32m      6\u001b[0m         \u001b[38;5;28mprint\u001b[39m(vowel)\n\u001b[1;32m----> 7\u001b[0m \u001b[43msearch4vowels\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: search4vowels() missing 1 required positional argument: 'word'"
     ]
    }
   ],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"Display any vowels found in an asked-for word.\"\"\"  # 为函数代码增加一个docstring，简要描述函数的用途\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    for vowel in found:\n",
    "        print(vowel)\n",
    "        \n",
    "search4vowels()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a5b1c45b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "u\n"
     ]
    }
   ],
   "source": [
    "search4vowels('wu')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "673eee69",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "search4vowels() takes 1 positional argument but 2 were given",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [8]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43msearch4vowels\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mwu\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mling\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: search4vowels() takes 1 positional argument but 2 were given"
     ]
    }
   ],
   "source": [
    "search4vowels('wu','ling')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6247731e",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "search4vowels() takes 1 positional argument but 2 were given",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [10]\u001b[0m, in \u001b[0;36m<cell line: 8>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m vowel \u001b[38;5;129;01min\u001b[39;00m found:\n\u001b[0;32m      6\u001b[0m         \u001b[38;5;28mprint\u001b[39m(vowel)\n\u001b[1;32m----> 8\u001b[0m \u001b[43msearch4vowels\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mwu\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mling\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: search4vowels() takes 1 positional argument but 2 were given"
     ]
    }
   ],
   "source": [
    "def search4vowels(words):\n",
    "    \"\"\"Display any vowels found in an asked-for word.\"\"\"  # 为函数代码增加一个docstring，简要描述函数的用途\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(words))\n",
    "    for vowel in found:\n",
    "        print(vowel)\n",
    "        \n",
    "search4vowels('wu','ling')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "730fd788",
   "metadata": {},
   "source": [
    "------------\n",
    "* 2022/11/13/week 12\n",
    "* Python学习记录\n",
    "* 记录人：wu-huichun\n",
    "-----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3ce75d7",
   "metadata": {},
   "source": [
    "# 函数返回一个结果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "507e32b9",
   "metadata": {},
   "source": [
    "### 1. return语句\n",
    "\n",
    "### 2. 内置函数-bool\n",
    "\n",
    "> 可以处理任何值和任何python对象\n",
    "\n",
    "> python中的“真值”：1代表True,0代表False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0ed2bfe",
   "metadata": {},
   "source": [
    "* 计算为0、值None、空串或一个空的内置数据结构，则为False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bd681173",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "69b833c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(0.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fef95738",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f65dea4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool([])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "33e62282",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool({})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ac1597bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(None)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "184eea7c",
   "metadata": {},
   "source": [
    "* Python中所有其他对象都计算为True,所有非空的数据结构都计算为True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b4016778",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2d8775e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ecb01996",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2ba6d720",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(0.00000000000000000000000000000000000009)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "49b0c899",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool('Panic')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "698eb65f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool([42,43,44])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c6d38b89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool({'a':42,'b':42})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5909dd99",
   "metadata": {},
   "source": [
    "### 3.返回一个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3d6f8b01",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"Display any vowels found in an asked-for word.\"\"\"  # 为函数代码增加一个docstring，简要描述函数的用途\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    #for vowel in found:\n",
    "        #print(vowel)\n",
    "    return bool(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ebf6a11a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels('hitch-hiker')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "78e4db20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels('sky')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7116de74",
   "metadata": {},
   "source": [
    "### 4.返回多个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d48f3093",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"Return a boolean based on any vowels found.\"\"\"  # 为函数代码增加一个docstring，简要描述函数的用途\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    return bool(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "01cac450",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels('hitch-hiker')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c4041c68",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"Return any vowels found in a supplied word.\"\"\"  # 更新注释\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    return found   # 结果作为一个数据结构（一个集合）返回"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8acaae46",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'i'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels('hitch-hiker')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2ada5d6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"Return any vowels found in a supplied word.\"\"\"  # 更新注释\n",
    "    vowels = set('aeiou')\n",
    "    return vowels.intersection(set(word))  # 使用数据，但没有使用不必要的“found”变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "7b9421b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'i'}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels('hitch-hiker')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3bac6eef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels('galaxy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1a014f0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels('sky')  # 空集合表示为set()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2193427",
   "metadata": {},
   "source": [
    "### 5.内置数据结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "187e1e5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 空列表\n",
    "l = list()\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "07b40df4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 空字典\n",
    "d = dict()\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "5fa6a764",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 空集合\n",
    "s = set()\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "cd6cdd35",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 空元组\n",
    "t = tuple()\n",
    "t"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d2bbfc7",
   "metadata": {},
   "source": [
    "### 6.使用注释改进文档"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86ca48b6",
   "metadata": {},
   "source": [
    "* 函数注释是可选的\n",
    "\n",
    "* 函数注释可以提供信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "a11981d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'u'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def search4vowels(word:str) -> set:\n",
    "    \"\"\"Return any vowels found in a supplied word.\"\"\" \n",
    "    vowels = set('aeiou')\n",
    "    return vowels.intersection(set(word))  \n",
    "search4vowels('wu')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d742f3bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function search4vowels in module __main__:\n",
      "\n",
      "search4vowels(word: str) -> set\n",
      "    Return any vowels found in a supplied word.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search4vowels)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40fa4ed4",
   "metadata": {},
   "source": [
    "### 7.建立一个通用的函数\n",
    "\n",
    "> A.为这个新函数指定一个更通用的名字\n",
    "\n",
    "> B.增加第二个参数\n",
    "\n",
    "> C.删除vowels变量\n",
    "\n",
    "> D.更新docstring"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "17d89182",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'i', 'u'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def search4vowels(phrase:str) -> set:\n",
    "    \"\"\"Return any vowels found in a supplied phrase.\"\"\" \n",
    "    vowels = set('aeiou')\n",
    "    return vowels.intersection(set(phrase))  \n",
    "search4vowels('huichun')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "a2474bb6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'u'}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "def search4vowels(phrase:str) -> set:\n",
    "    \"\"\"Return any vowels found in a supplied phrase.\"\"\" \n",
    "    vowels = set('aeiou')\n",
    "    return vowels.intersection(set(phrase))  \n",
    "\n",
    "def search4letters(phrase:str, letters:str) -> set:\n",
    "    \"\"\"Return a set of the 'letters' found in 'phrase'.\"\"\" \n",
    "    return set(letters).intersection(set(phrase))  \n",
    "\n",
    "search4letters('huichun','wu')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "69fc6fc1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4letters('huichun,ni,life','o')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66be9a8a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
